/*
    Copyright (C) 2011  Hugo Arregui, FuDePAN

    This file is part of the FuDePAN Extensible Cache

    Feca is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Feca is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Feca.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef FECA_STORAGE_POLICIES_H
#define FECA_STORAGE_POLICIES_H

#include <mili/mili.h>

template<class Key, class CacheableObject>
class MemStorage
{
    typedef std::map<Key, CacheableObject> Map;
public:
    bool contains(const Key& key) const
    {
        return mili::contains(_cache, key);
    }
    size_t size() const
    {
        return _cache.size();
    }
    bool empty() const
    {
        return _cache.empty();
    }
    void clear()
    {
        _cache.clear();
    }

    CacheableObject* get(Key id)
    {
        typename Map::iterator it = _cache.find(id);
        if (it == _cache.end())
        {
            return NULL;
        }
        else
        {
            return &(it->second);
        }
    }
    void insert(Key id, CacheableObject& object)
    {
        insert(std::pair<Key, CacheableObject>(id, object));
    }
    void insert(const std::pair<Key, CacheableObject>& p)
    {
        _cache.insert(p);
    }
private:
    Map _cache;
};
#endif
